<?php
namespace Home\Controller;
use Think\Controller;
class StudentController extends BaseController {
    /**
     * 主页跳转
     * @return [type] [description]
     */
    public function index(){
        if (session('sid')) {
            $this->redirect('Student/lists');
        } else {
            $this->login();
        }
        
    }
    /**
     * 添加学生 override
     */
    public function add()
    {
        $class_id=I('class_id',0,'intval');

        if (IS_POST) {
            $this->reg();
        } else {
            $this->assign('class_id',$class_id);
            $this->assign('classes',D('Class')->findAll());
            $this->display();
        }
        
    }
    /**
     * 学生注册 事务 更改班级人数
     * @return [type] [description]
     */
    public function reg()
    {
        if (IS_POST && I('post.user_name')) {
            $user=D('Student');
            $user->create();
            $user->startTrans();
            if (!is_null($user) && !$user->getError()) {
                if ($user->add() && M('Class')->where(array('id'=>I('class_id',0,'intval')))->setInc('number')) {
                    // echo 'success!';
                    $user->commit();
                    $this->success('注册成功！',U('Student/login'));
                } else {
                    $user->rollback();
                    // echo 'reg fail!!!!<br>';
                    //var_dump($user);
                    $this->error('注册失败！！');
                }
            } else {
                echo($user->getError());
            }
        } else {
            $this->assign('classes',D('Class')->findAll());
            $this->display();
        }
    }

    public function profile()
    {
        $id=session('sid');
        if ($id) {
            $profile=D('Student')->find_by_id($id);
            var_dump($profile);
        } else {
            $this->show('<script>alert("无权查看");</script>');
        }
    }
    /**
     * 学生登陆查看profile
     * @return [type] [description]
     */
    public function login()
    {
        if (IS_POST) {
            $student=M('Student');
            $map=I('post.');
            $map['password']=md5($map['password']);
            $rst = $student->where($map)->find();
            if ($rst) {
                session('sid',$rst['id']);
                // echo 'login success!';
                $this->success('登陆成功！',U('Student/profile'));
            } else {
                // echo '用户名或密码错误！';
                $this->error('用户名或密码错误！');
            }
            
        } else {
            $this->display('login');
        }
        
    }
    /**
     * 根据班级id列出该班级学生
     * @return [type] [description]
     */
    public function list_by_class_id()
    {
        $class_id=I('class_id',0,'intval');
        $map=array(
            'class_id'=>$class_id,
            'is_delete'=>0,
            'status'=>array('egt',0)
            );
        $M=D(CONTROLLER_NAME);

        $count=$M->countMap($map);
        $page=new \Think\Page($count,10);

        $Ms=$M->listMap($map,$page->firstRow.','.$page->listRows);

        $this->assign('Ms',$Ms);
        $this->assign('page',$page->show());

        $this->display('lists');
    }
    /**
     * 列举所有信息 模型视图
     * @return [type] [description]
     */
    public function list_all_info()
    {
        $map=array(
            'is_delete'=>0,
            'status'=>array('egt',0)
        );
        $M=D('StudentsView');

        $count=$M->countMap($map);
        $page=new \Think\Page($count,10);

        $Ms=$M->listMap($map,$page->firstRow.','.$page->listRows);

        $this->assign('Ms',$Ms);
        $this->assign('page',$page->show());

        $this->display();
    }

}